package cn.kjcoder.springcloud.controller;

import cn.kjcoder.springcloud.domain.JsonResult;
import cn.kjcoder.springcloud.domain.Payment;
import cn.kjcoder.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * @author hkj
 * @email 1349091387@qq.com
 * @date 2021/12/20
 * 源码允许修改，并且本人十分乐意有 大佬 来对代码进行 随 意 调 教
 **/
@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {

    @Autowired
    private PaymentService paymentService;
    @Value("${server.port}")
    private String port;

    @PostMapping("/save")
    public JsonResult save(@RequestBody Payment payment){
        int rs = paymentService.save(payment);
        log.info("插入结果：" + rs);
        if(rs > 0){
            return JsonResult.success("插入数据库成功。port:" + port);
        }else{
            return JsonResult.error(444,"插入数据库失败",null);
        }
    }

    @GetMapping("/get/{id}")
    public JsonResult get(@PathVariable("id")Long id){
        Payment paymentById = paymentService.getPaymentById(id);
        log.info("查询结果：" + paymentById + "O(∩_∩)O哈哈~");
        if(paymentById != null){
            return JsonResult.success(paymentById + "查询数据库成功。port:" + port);
        }else{
            return JsonResult.error(444,"插入数据库失败",null);
        }
    }


}
